﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Brettle.Web.NeatUpload;
using System.IO;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data;

public partial class systemconfig_Upload_img : System.Web.UI.Page
{
    public string UpPath = "/picture/file/" + String.Format("{0}/{1}/{2}/", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["sJigou"] == null || Session["sJigou"].ToString() == string.Empty)
        {
            return;
        }
        mapJigouId();
        //if (IsPostBack)
        //{
            DataBrid();
        //}
    }



    /// <summary>
    /// 读取列表
    /// </summary>
    private void DataBrid()
    {
        string sqlText = string.Empty;

            sqlText = string.Format("select Id,JiGouBiaoZhiFu,LuJing,img_order,font_img,visible=case visible when 0 then '不显示'  when 1 then '显示'  end from TuPIanKu where JiGouBiaoZhiFu ='{0}' order by img_order desc ", Session["JigouId"].ToString());
       
        imglist.DataSource = DbHelper.ExecuteTable(CommandType.Text, sqlText, null);
        imglist.DataBind();
    }



    /// <summary>
    /// 机构名称映射到唯一id
    /// </summary>
    void mapJigouId()
    {
        string sql = string.Format("select count(id) from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"].ToString());
        int num = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
        if (num == 0)
        {
            sql = string.Format("insert into JiGouPeiZhi (JiGouMingCheng) values('{0}')", Session["sJigou"].ToString());
            num = DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            if (num == 1)
            {
                sql = string.Format("select id from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"].ToString());
                Session["JigouId"] = DbHelper.ExecuteScalar(CommandType.Text, sql, null);
            }
            else
            {
                Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "机构映射失败"));
            }
        }
        else
        {
            sql = string.Format("select id from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"].ToString());
            Session["JigouId"] = DbHelper.ExecuteScalar(CommandType.Text, sql, null);
        }
    }

    //添加图片内容
    protected void btn_add_Click(object sender, EventArgs e)
    {
        string imgname = this.img_title.Text.Trim();
        string uploadname = this.filename.FileName;
        if (this.filename.FileName != null)
        {
            string file_extend = new FileInfo(this.filename.FileName).Extension.ToLower();
            if (file_extend != ".jpg")
            {
                Response.Write("<script>alert('请选择jpg格式的图片');location.href='Upload_img.aspx'</script>");
                return;
            }
            filename.MoveTo(GetPath() + uploadname, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
            uploadname = UpPath + uploadname;
        }
        else
        {
            Response.Write("<script>alert('请选择图片');location.href='Upload_img.aspx'</script>");
            return;
        }
        string sqlText = "insert into TuPIanKu(img_order,JiGouBiaoZhiFu,LuJing,font_img,visible)values(@img_order,@JiGouBiaoZhiFu,@LuJing,@font_img,@visible)";
        SqlParameter[] param = { new SqlParameter("@img_order",SqlDbType.Int,4),
                                 new SqlParameter("@JiGouBiaoZhiFu",SqlDbType.Int),
                                 new SqlParameter("@LuJing",SqlDbType.VarChar,100),
                                 new SqlParameter("@font_img",SqlDbType.NVarChar,50),
                                 new SqlParameter("@visible",SqlDbType.Int,4)
                                };
        param[0].Value = Convert.ToInt32(order_Index.Text.Trim());
        param[1].Value = int.Parse(Session["JigouId"].ToString());
        param[2].Value = uploadname;
        param[3].Value = img_title.Text.Trim();
        param[4].Value = Convert.ToInt32(this.show.SelectedValue);

        int id = DbHelper.ExecuteNonQuery(CommandType.Text, sqlText, param);
        if (id > 0)
        {
            Response.Write("<script>alert('添加成功');location.href='Upload_img.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('添加失败');location.href='Upload_img.aspx'</script></script>");
        }

    }



    public string GetPath()
    {

        string path = Server.MapPath("~") + UpPath;
        if (!Directory.Exists(path))
        {
            try
            {
                Directory.CreateDirectory(path);
            }

            catch
            {
            }
        }
        return path;
    }
}
